package com.itaem.community.modules.pay.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itaem.community.core.annotations.ResponseResult;
import com.itaem.community.modules.pay.dto.PayEntityDto;
import com.itaem.community.modules.pay.entity.PayEntity;
import com.itaem.community.modules.pay.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * <p>
 * 缴费 前端控制器
 * </p>
 *
 * @author tomato
 * @since 2020-10-28
 */
@Api(tags = "支付模块")
@ResponseResult
@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private PayService payService;

    /**
     *
     * @param page 当前页数是第page页
     * @param size 每页最多有size个数据
     * @return page对象
     */
//    @RequiresRoles(value = {"超级管理员"})
    @ApiOperation("分页查询信息")
    @GetMapping("/{page}/{size}")
    public Page list(@PathVariable Long page, @PathVariable Long size){
        Page<PayEntity> payEntityPage = new Page<PayEntity>(page,size);
        return payService.page(payEntityPage,new QueryWrapper<PayEntity>().eq("deleted",0));
    }

    /**
     *
     * @param id 获取id为id的信息
     * @return
     */
//    @RequiresRoles(value = {"超级管理员"})
    @ApiOperation("获取单条信息")
    @GetMapping("/{id}")
    public PayEntity info(@PathVariable Long id){
        return payService.getById(id);
    }

    /**
     *
     * @param payEntityDto 前端需要传入的数据
     * @return
     */
//    @RequiresRoles(value = {"超级管理员"})
    @ApiOperation("新增")
    @PostMapping
    public Boolean save(@RequestBody PayEntityDto payEntityDto) {
        PayEntity payEntity = new PayEntity();
        BeanUtils.copyProperties(payEntityDto,payEntity);
        payEntity.setStatus(0);
        payEntity.setPayTime(LocalDateTime.now());
        payService.save(payEntity);
        return true;
    }

    /**
     *
     * @param payEntityDto 前端需要传入的数据
     * @return
     */
//    @RequiresRoles(value = {"超级管理员"},logical = Logical.OR)
    @ApiOperation("更新")
    @PutMapping
    public Boolean update(@RequestBody PayEntityDto payEntityDto) {
        PayEntity payEntity = new PayEntity();
        BeanUtils.copyProperties(payEntityDto,payEntity);
        payService.update(payEntity,new UpdateWrapper<PayEntity>().eq("id",payEntity.getId()));
        return true;
    }

    /**
     * 
     * @param id 需要删除的数据 (逻辑删除)
     * @return
     */
//    @RequiresRoles(value = {"超级管理员"},logical = Logical.OR)
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public Boolean remove(@PathVariable Long id) {
        payService.update(new UpdateWrapper<PayEntity>().set("deleted",1).eq("id",id));
        return true;
    }
}

